//
//  NGForumTableView.swift
//  NingguoForum
//
//  Created by zhengsw on 2025/7/17.
//  Copyright © 2025 zhengsw. All rights reserved.
//

import UIKit
import SnapKit

// MARK: - Protocol
protocol NGForumTableViewDelegate: AnyObject {
    func forumTableView(_ tableView: NGForumTableView, didSelectItemAt indexPath: IndexPath, in tabIndex: Int)
    func forumTableView(_ tableView: NGForumTableView, didScrollToTab index: Int)
    func forumTableView(_ tableView: NGForumTableView, didTriggerRefreshIn tabIndex: Int)
    func forumTableView(_ tableView: NGForumTableView, didTriggerLoadMoreIn tabIndex: Int)
}

class NGForumTableView: UIView {
    
    // MARK: - Properties
    private var titles: [ResponseForumDetailListModel]
    private var tableViews: [NGForumSingleTableView] = []
    private var selectedIndex = 0
    var paramters = NSMutableDictionary()

    weak var delegate: NGForumTableViewDelegate?
    
    // MARK: - UI Components
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.delegate = self
        scrollView.backgroundColor = .white
        return scrollView
    }()
    
    // MARK: - Initialization
    init(titles: [ResponseForumDetailListModel]) {
        self.titles = titles
        super.init(frame: .zero)
        setupUI()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public func configTitles(titles: [ResponseForumDetailListModel], paramters: NSDictionary) {
        self.titles = titles;
        self.paramters = paramters as! NSMutableDictionary;
        setupTableViews()
    }
    
    // MARK: - UI Setup
    private func setupUI() {
        addSubview(scrollView)
        
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    private func setupTableViews() {
        let width = bounds.width > 0 ? bounds.width : UIScreen.main.bounds.width
        
        for i in 0..<1 {
            let model = titles[i];
            
            let tableView = NGForumSingleTableView(tabIndex: i, title: model.name!, paramters: self.paramters)
            tableView.delegate = self
            
            scrollView.addSubview(tableView)
            tableViews.append(tableView)
            
            tableView.frame = CGRect(x: CGFloat(i) * width, y: 0, width: width, height: bounds.height > 0 ? bounds.height : 400)
        }
        
        scrollView.contentSize = CGSize(width: width * CGFloat(titles.count), height: bounds.height > 0 ? bounds.height : 400)
    }
    
    // MARK: - Public Methods
    func selectTab(at index: Int) {
        selectedIndex = index
        let offset = CGFloat(index) * scrollView.bounds.width
        scrollView.setContentOffset(CGPoint(x: offset, y: 0), animated: true)
    }
    
    func reloadData() {
        for tableView in tableViews {
            tableView.reloadData()
        }
    }
    
    func reloadData(for tabIndex: Int) {
        guard tabIndex < tableViews.count else { return }
        tableViews[tabIndex].reloadData()
    }
    
    func endRefreshing(for tabIndex: Int) {
        guard tabIndex < tableViews.count else { return }
        tableViews[tabIndex].endRefreshing()
    }
    
    func endLoadMore(for tabIndex: Int) {
        guard tabIndex < tableViews.count else { return }
        tableViews[tabIndex].endLoadMore()
    }
    
    // MARK: - Layout
    override func layoutSubviews() {
        super.layoutSubviews()
        updateScrollViewContentSize()
    }
    
    private func updateScrollViewContentSize() {
        let width = bounds.width
        let height = bounds.height
        
        for (i, tableView) in tableViews.enumerated() {
            tableView.frame = CGRect(x: CGFloat(i) * width, y: 0, width: width, height: height)
        }
        
        scrollView.contentSize = CGSize(width: width * CGFloat(titles.count), height: height)
    }
}

// MARK: - UIScrollViewDelegate
extension NGForumTableView: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        selectedIndex = page
        delegate?.forumTableView(self, didScrollToTab: page)
    }
    
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        selectedIndex = page
        delegate?.forumTableView(self, didScrollToTab: page)
    }
}

// MARK: - NGForumSingleTableViewDelegate
extension NGForumTableView: NGForumSingleTableViewDelegate {
    func forumSingleTableView(_ tableView: NGForumSingleTableView, didSelectItemAt indexPath: IndexPath) {
        delegate?.forumTableView(self, didSelectItemAt: indexPath, in: tableView.tabIndex)
    }
    
    func forumSingleTableViewDidTriggerRefresh(_ tableView: NGForumSingleTableView) {
        delegate?.forumTableView(self, didTriggerRefreshIn: tableView.tabIndex)
    }
    
    func forumSingleTableViewDidTriggerLoadMore(_ tableView: NGForumSingleTableView) {
        delegate?.forumTableView(self, didTriggerLoadMoreIn: tableView.tabIndex)
    }
}
